package com.angysnail.getopt;

import gnu.getopt.Getopt;
import gnu.getopt.LongOpt;

public class GetComPram {

	public static void main(String[] args) {
		if (args.length <= 1) {
			System.out.println("你至少需要一个参数作为选项！");
			return;
		}
		String str = "";
		int ch;
		LongOpt[] longopts = new LongOpt[3];// 使用LongOpt设置长选项参数
		longopts[0] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h');
		longopts[1] = new LongOpt("username", LongOpt.REQUIRED_ARGUMENT, null, 'U');
		longopts[2] = new LongOpt("password", LongOpt.REQUIRED_ARGUMENT, null, 'P');
		Getopt getopt = new Getopt("dsp", args, "-:U:P:h", longopts);
		getopt.setOpterr(false);// 我们自己进行错误处理
		while ((ch = getopt.getopt()) != -1) {
			switch (ch) {
			case 'U':
				str = getopt.getOptarg();
				System.out.println("username = " + ((str != null) ? str : "null"));
				break;
			case 'P':
				str = getopt.getOptarg();
				System.out.println("password " + ((str != null) ? str : "null"));
				break;
			case 'h':
				System.out.println("dsp系统进入必须输入的参数：-U/--username:输入用户名  -P/--password：输入密码");
				break;
			case ':':
				System.out.println("你至少需要一个参数作为选项！");
				break;
			case '?':
				System.out.println("这个选项不合法，请使用--help/-h查看相关帮助！");
				break;
			default:
				;
			}
		}
	}
}
